<?php
class Default_Model_TinNhanMapper {

	protected $_dbTable;
	protected $db;

	public function setDbTable($dbTable) {
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable() {
		if (null === $this->_dbTable) {
			$this->setDbTable('Default_Model_DbTable_TinNhan');
		}
		return $this->_dbTable;
	}
	
	public function getDb() {
		if ($this->db === null) {
			$this->db=Zend_Registry::get('connectDB');
		}
		return $this->db;
	}
	
	/*
	 * count item
	 */
	public function countItem($query = null) {
		$table = $this->getDbTable();
		$select = $table->select();
		$select->from($table, array("count(id) as totalItem"));
		if ($query != null)
        	$select->where($query);
        
		$resultSet = $table->fetchAll($select);
		$array = $resultSet->toArray();
		return $array[0]['totalItem'];
	}

	/*
	 * save function
	 */
	public function save(Default_Model_TableColumn_TinNhan $item, $task = null) {
		$now = date("Y-m-d H:i:s",time());
		try {
			$data  = array(
				'id'		    => $item->getId(),
				'nguoi_nhan'    => $item->getNguoi_nhan(),
				'nguoi_gui'     => $item->getNguoi_gui(),
				'ngay_gui' 	    => $item->getNgay_gui(),
				'tieu_de' 	    => $item->getTieu_de(),
				'noi_dung'	    => $item->getNoi_dung(),
				'loai'		    => $item->getLoai(),
				'ptr_tin_rao'	=> $item->getPtr_tin_rao(),
				'ogh_tin_rao'	=> $item->getOgh_tin_rao(),
			);
			$data['ngay_gui'] = $now;
			if ($item->getId() > 0) {
				$id = $item->getId();
				$this->getDbTable()->update($data, $this->getDbTable()->getAdapter()->quoteInto('id = ?', $id));
			} else {
				unset($data['id']);
				$this->getDbTable()->insert($data);
			}
				
			return TRUE;
		} catch (Exception $e) {
			return FALSE;
		}
	}
	
	/*
	 * fetch
	 */
	public function fetch($arrParam = null, $query = null, $num_new = 0, $colOrder = array(array('colName' => 'ngay_gui', 'order' => 'DESC')), $option = 'view') {
		
		$table = $this->getDbTable();
        $select = $table->select();
        $select->from($table);
        
        if ($query != null)
        	$select->where($query);

        if (isset($arrParam['ssFilter'])) {
			$ssFilter = $arrParam['ssFilter'];
	
			if (!empty($ssFilter['col']) && !empty($ssFilter['order'])) {
				$col = $ssFilter['col'];
				$order = $ssFilter['order'];
				$select->order(array($col . ' ' . $order));
			}
        }
		if ($option != 'view') {
			$paginator = $arrParam['paginator'];
			$keywords = '';
		
			if ($num_new > 0) {
				$select->limitPage(0, $num_new);
			} elseif ($paginator['itemCountPerPage'] > 0) {
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page, $rowCount);
			}
		
			$order = array();
			foreach ($colOrder as $val) {
				array_push($order, $val['colName'] . ' ' . $val['order']);
			}
		}
		
		$resultSet = $this->getDbTable()->fetchAll($select);
		return $this->processResultSet($resultSet->toArray(), $option);

	}
	
	/*
	 *processResultSet
	 */
	private function processResultSet($resultSet, $option = 'view') {
		$entries = "";
		if($option == 'view') {
			foreach ($resultSet as $row) {
				$entries = new Default_Model_TableColumn_TinNhan($row);
			}
		} else {
			$entries = array();
			foreach ($resultSet as $row) {
				$entry = new Default_Model_TableColumn_TinNhan($row);
				$entries[] = $entry;
			}
		}
		return $entries;
	}

	/*
	 * delete function
	 */
	public function delete($id = 0, $key = 'id') {
		$where = $key . ' IN (' . $id . ')';
		$this->getDbTable()->delete($where);
	}
	
	/*
	 * change item
	 */
	public function change($id, $data = null) {
		$where = 'id = ' . $id;
		$this->getDbTable()->update($data, $where);
	}
	
}